/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bei.lingluan.slice;


import com.bei.lingluan.RecordingAbility;
import com.bei.lingluan.ResourceTable;


import com.bei.lingluan.utils.PreferencesHelper;
import com.bei.lingluan.utils.ResourceHelper;
import ohos.aafwk.ability.AbilitySlice;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.VectorElement;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * @author Kirky
 */
public class MainAbilitySlice extends AbilitySlice {
    private final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbilitySlice");
    private PreferencesHelper mPermissionPreferences, mUserSettingPreferences;
    private Image mNetwork, mAi, mMicrophone, mStorage;
    private Component action;
    private boolean mAct = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mNetwork = findComponentById(ResourceTable.Id_network_main);
        mAi = findComponentById(ResourceTable.Id_ai_main);
        mMicrophone = findComponentById(ResourceTable.Id_microphone_main);
        mStorage = findComponentById(ResourceTable.Id_storage_main);
        buttonListener();
    }

    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    protected void onBackground() {
        super.onBackground();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();

    }


    private void startAbility(String abilityName) {
        System.out.println(abilityName);
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.bei.lingluan")
                .withAbilityName(abilityName)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    /**
     * The icon changes color only when the permission is obtained and
     * relevant functions are started
     *
     * @author kirky
     */
    public void statusListener() {
        if (mUserSettingPreferences.getBoolean("storage")){
            VectorElement vectorElement = new VectorElement(getContext(), ResourceTable.Graphic_ic_ai_on);
            mStorage.setBackground(vectorElement);
        }
    }

    /**
     * @author kirky
     */
    public void buttonListener() {
//        setting button
        Component setting = findComponentById(ResourceTable.Id_setting_main);
        setting.setClickedListener(component -> present(new SettingAbilitySlice(), new Intent()));

//        power button
        action = findComponentById(ResourceTable.Id_power_main);
        action.setClickedListener(component -> {
            if (!mAct) {
//                present(new RecordingAbilitySlice(), new Intent());
                startAbility(RecordingAbility.class.getName());
                mAct = true;
            } else {
                mAct = false;
            }

        });

//        exit button
        Component exit = findComponentById(ResourceTable.Id_exit_main);
        exit.setClickedListener(component -> terminateAbility());
    }


}
